// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************


#include <sys/file.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <sstream>

#include <Ice/Ice.h>
#include "myconfig.h"
#include "logfile.h"
#include "MylogWorkQueue.h"

using namespace std;


MylogWorkQueue::MylogWorkQueue() :
	_done(false)
{
}

	void
MylogWorkQueue::run()
{
	IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);

	while(!_done)
	{
		char log_content[1024];

		if(_mylogqueue.size() == 0)
		{
			_monitor.wait();
		}

		if(_mylogqueue.size() != 0)
		{


			if(!_done)
			{


				//_monitor.timedWait(IceUtil::Time::milliSeconds(1000));
				_monitor.timedWait(IceUtil::Time::seconds(1));
				std::list<MylogQueue> _myqueue_data = _mylogqueue;
				_mylogqueue.clear();	


				string client_ip;
			
				for(std::list<MylogQueue>::iterator p = _myqueue_data.begin(); p != _myqueue_data.end(); ++p)
				{

					client_ip = p->ClientInfo;
					
					map<string,int>::iterator iter2;
					iter2 = m_ClientIp.find(client_ip);
					if(iter2->second >0){
						m_ClientIp.insert(pair<string,int>(client_ip,iter2->second++));
					}else{
						m_ClientIp.insert(pair<string,int>(client_ip,1));
					}


				}


				time_t curtime;
				curtime = time(0);
				char my_logfilename[1024];
				strftime(my_logfilename,1023,"./log/monitor_%Y%m%d.log",localtime(&curtime));
				char logtime[1024];
				strftime(logtime,1024,"%Y-%m-%d %H:%M:%S",localtime(&curtime));
				FILE * log_fp;


				string client_ip_list;
				map<string,int>::iterator  iter;
				ostringstream   oss;  

				int queue_i = 0;	
				for(iter = m_ClientIp.begin();iter != m_ClientIp.end(); iter ++)
				{
					queue_i = queue_i + iter->second;
					oss<< iter->second;

					if(client_ip_list.length()>0){
						client_ip_list = client_ip_list + "," + iter->first + ":" + oss.str();

					}else{

						client_ip_list = iter->first + ":" + oss.str();
					}
					oss.str("");
				}

				log_fp = fopen(my_logfilename,"a+");

				if(log_fp != NULL ){
					//fprintf (log_fp, "[%s] [%d] [%s]\n", logtime,_myqueue_data.size(),client_ip_list.c_str() ) ;
					fprintf (log_fp, "[%s] [%d] [%s]\n", logtime,queue_i,client_ip_list.c_str() ) ;
				}	
				fclose(log_fp);

				m_ClientIp.clear();
				_myqueue_data.clear();

			}
		}
	}

}

	void 
MylogWorkQueue::add(const std::string& ClientInfo)
{
	IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);

	if(!_done)
	{
		//
		// Add work item.
		//
		MylogQueue entry;
		entry.ClientInfo = ClientInfo;

		if(_mylogqueue.size() == 0)
		{
			_monitor.notify();
		}
		_mylogqueue.push_back(entry);
	}
	else
	{
		//
		// Destroyed, throw exception.
		//
		//cb->ice_exception(AdCtr::RequestCanceledException());
	}
}

	void
MylogWorkQueue::destroy()
{
	IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);

	//
	// Set done flag and notify.
	//
	_done = true;
	con.disconnect();
	_monitor.notify();
}

